package org.judking.CarKeeper.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;


import android.util.Log;

public class SynFileOpreService {
	private String path;
	private  RandomAccessFile raf;
	private static final String BASIC_DATA_PATH = ResourceService.BASE_PATH+"cache";
	
	public static SynFileOpreService BASIC_DATA_CACHE = new SynFileOpreService(BASIC_DATA_PATH);
	
	public static void reset()			{
		BASIC_DATA_CACHE = new SynFileOpreService(BASIC_DATA_PATH);
	}

	private SynFileOpreService(String path)		{
		try {
			this.path = path;
			raf = new RandomAccessFile(path, "rw");
		} catch (FileNotFoundException e) {
			Log.i("judking", "SynFileOpreService: FileNotFoundException : " + e);
		}
		
	}

	
	public void synAppend(String content) throws IOException		{
		synchronized (raf) {
			raf.seek(raf.length());
			raf.write(content.getBytes());
		}
	}
	
	public String synGetNClear() throws IOException		{
		synchronized (raf) {
			if(raf.length() <= 0)		{
				return "";
			}
			byte[] buffer = new byte[(int)raf.length()];
			raf.seek(0);
			int size = raf.read(buffer);
			raf.setLength(0);
			return new String(buffer, 0, size);
		}
	}
	
}
